package com.lagou.filter;

import java.util.*;

public class SingleThread extends Thread {
    private static SingleThread singleThread;
    public static boolean flag = false;

    private static List<Long> methodAList;
    private static List<Long> methodBList;
    private static List<Long> methodCList;



    public SingleThread() {
    }

    public SingleThread(List<Long> methodAList,List<Long> methodBList,List<Long> methodCList) {
        this.methodAList = methodAList;
        this.methodBList = methodBList;
        this.methodCList = methodCList;

        singleThread = this;
    }

    public static List<Long> getMethodAList() {
        return methodAList;
    }

    public static void setMethodAList(List<Long> methodAList) {
        SingleThread.methodAList = methodAList;
    }

    public static List<Long> getMethodBList() {
        return methodBList;
    }

    public static void setMethodBList(List<Long> methodBList) {
        SingleThread.methodBList = methodBList;
    }

    public static List<Long> getMethodCList() {
        return methodCList;
    }

    public static void setMethodCList(List<Long> methodCList) {
        SingleThread.methodCList = methodCList;
    }

    public static SingleThread getInstance(){
        if (singleThread== null){
            singleThread = new SingleThread();
        }
        return singleThread;
    }
    @Override
    public void run() {
        flag = true;
        while (true){
            try {
                Thread.sleep(5000l);
                System.out.println("=======================");
                //方法A
                printTP90TP99("methodA",methodAList);

                //方法C
                printTP90TP99("methodB",methodBList);

                //方法C
                printTP90TP99("methodC",methodCList);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    private void printTP90TP99(String methodName,List<Long> methodList) {
        List<Long> list = methodList;
        int size = list.size();
        list.sort(Comparator.naturalOrder());
        int index90 = (int) Math.floor(size*0.9);
        int index99 = (int) Math.floor(size*0.99);
        System.out.println(methodName + "  TP90: " + list.get(index90) +" 毫秒 TP99: " + list.get(index99) + " 毫秒");
    }
}
